package com.bhs.sansong.ui.wallet;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.UserInfo;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.setting.RealNameAuthenticationActivity;
import com.bhs.sansong.utils.ToastUtil;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 余额
 */
public class BalanceActivity extends BaseActivity implements View.OnClickListener, NetCallBack {


    private TextView mTvBalance;
    private String balance;
    private int is_approve = -1;
    private TextView mTvHint;
    private TextView mTvWithdrawDeposit;
    private TextView mTvVoucher;
    private String draw_status;

    @Override
    public int initLayout() {
        return R.layout.activity_balance;
    }

    @Override
    public void initView() {

        findViewById(R.id.tv_bill).setOnClickListener(this);
        mTvBalance = findViewById(R.id.tv_balance);
        mTvWithdrawDeposit = findViewById(R.id.tv_withdraw_deposit);
        mTvWithdrawDeposit.setOnClickListener(this);
        mTvBalance.setText(String.format("￥%s", getIntent().getStringExtra("balance")));
        mTvHint = findViewById(R.id.tv_hint);
        mTvVoucher = findViewById(R.id.tv_voucher);
        mTvVoucher.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tv_bill:
                startActivity(new Intent(mActivity, MyBillActivity.class)
                        .putExtra("type", "money01"));
                break;
            case R.id.tv_voucher:
                startActivity(new Intent(mActivity, TopUpActivity.class));
                break;
            case R.id.tv_withdraw_deposit:
                voucher();
                break;
        }
    }

    private void voucher() {
        if (is_approve == 1) {
            if (!draw_status.equals("1")) {
                startActivity(new Intent(mActivity, WithdrawDepositActivity.class).putExtras(getIntent())
                        .putExtra("balance", balance));
            }else {
                new XPopup.Builder(mActivity)
                        .asConfirm("提示", "当前账户无法进行提现，请联系平台客服!",
                                "取消", "确定",
                                new OnConfirmListener() {
                                    @SuppressLint("NotifyDataSetChanged")
                                    @Override
                                    public void onConfirm() {
                                    }
                                }, null, false)
                        .show();
            }
        } else if (is_approve == -1) {
            ToastUtil.show("正在获取用户数据");
        } else {
            new XPopup.Builder(mActivity)
                    .asConfirm("提示", "你还未实名认证，现在去认证",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @SuppressLint("NotifyDataSetChanged")
                                @Override
                                public void onConfirm() {
                                    startActivity(new Intent(mActivity, RealNameAuthenticationActivity.class));
                                }
                            }, null, false)
                    .show();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        networkRequest(netApi.get_user_info(), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        UserInfo infoBean = new Gson().fromJson(json, UserInfo.class);
        if (infoBean.isStatus()) {
            UserInfo.DataDTO.UserInfoDTO companyInfo = infoBean.getData().getUser_info();
            mTvBalance.setText(String.format("￥%s", companyInfo.getMoney01()));
            balance = companyInfo.getMoney01();
            is_approve = companyInfo.getIs_approve();
            draw_status = companyInfo.getDraw_status();
            if (companyInfo.getWallet_status().equals("1")) {
                mTvHint.setVisibility(View.VISIBLE);
                mTvWithdrawDeposit.setEnabled(false);
                mTvWithdrawDeposit.setBackgroundResource(R.drawable.bg_grey_8);
                mTvVoucher.setEnabled(false);
                mTvVoucher.setBackgroundResource(R.drawable.bg_grey_8);
                mTvWithdrawDeposit.setTextColor(Color.parseColor("#ffffff"));
            }

        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}